package com.csd.action.message;

import java.util.ArrayList;
import java.util.List;

import com.csd.helper.ActionResult;
import com.csd.helper.IConstant;
import com.csd.helper.SessionManager;
import com.csd.pojo.Message;
import com.csd.service.IMessageService;

public class MessageSendListAction {
	
	public Integer getNumInPage() {
		return numInPage;
	}

	public void setNumInPage(Integer numInPage) {
		this.numInPage = numInPage;
	}

	public Integer getCurrPage() {
		return currPage;
	}

	public void setCurrPage(Integer currPage) {
		this.currPage = currPage;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	private Integer numInPage = IConstant.numInPage;

	private Integer currPage;
	
	private Integer totalPage;

	private List<Message> messageSendList;
	private String type;
	private IMessageService messageService;

	public IMessageService getMessageService() {
		return messageService;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}

	public List<Message> getMessageSendList() {
		return messageSendList;
	}

	public void setMessageSendList(List<Message> messageSendList) {
		this.messageSendList = messageSendList;
	}

	public String execute() {

		String result = ActionResult.fail;
		Integer id = null;
		try {
			id = SessionManager.getUserID();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (id == null)
			return result;
		
		List<Message> old = messageService.listSendMessage();;
		
		if( this.currPage == null  || this.currPage <= 0 )
			this.currPage = 1;
		
		if( this.numInPage == null || this.numInPage <= 0)
			this.numInPage = IConstant.numInPage;
		
		if(messageSendList == null)
		{
			messageSendList = new ArrayList<Message>();
		}
		
		if(old == null)
		{
			old = new ArrayList<Message>();
		}
		
		messageSendList.clear();
		
		for(int i = (this.currPage-1)*this.numInPage; 
				i < this.currPage*this.numInPage && i < old.size(); 
				i++)
		{
			messageSendList.add( old.get(i) );
		}
		

		this.totalPage = old.size() / this.numInPage;
		
		if(old.size() % this.numInPage != 0)
			this.totalPage++;

		result = ActionResult.success;
		
		return result;
	}

}
